package com.blockmeta.bbs.immodule.corebean.Impl;

import android.content.Context;
import android.text.TextUtils;

import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.corebean.IDomainCommonManger;
import com.blockmeta.bbs.immodule.dao.CircleMember;
import com.blockmeta.bbs.immodule.dao.IMUserEntity;
import com.blockmeta.bbs.immodule.greendao.repository.IChatMessageRepo;
import com.blockmeta.bbs.immodule.greendao.repository.IUserRepo;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * service业务通用能力
 * Manager ，《阿里巴巴 Java 开发手册》中定义如下:
 * 对第三方平台封装的层，预处理返回结果及转化异常信息
 * 对 Service 层通用能力的下沉，如缓存方案、中间件通用处理
 * 与 DAO 层交互，对多个 DAO 的组合复用
 */
public class DomainCommonManger implements IDomainCommonManger {
    final Context mContext;
    IUserRepo userRepo;
    IChatMessageRepo chatMessageRepo;

    public static final String REGEX = "\\{\\$(\\w*):(\\w*)\\}"; //定义HTML标签的正则表达式
    public static final Pattern p= Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);

    public static final String userDeeplink = "<a href=\"blockmeta://im/user?id=";

    public DomainCommonManger(Context context) {
        mContext = context;
        chatMessageRepo = IMRepoContext.getChatMessageRepo(context);
        userRepo = IMRepoContext.getIMUserRepo(context);
    }

    @Override
    public int getUnCheckedMsgCount(long uidInt) {
        List<Long> noDisturbGroupIds = userRepo.getNoDisturbGroupIds(uidInt);
        int count = chatMessageRepo.getUncheckedMessageCountExceptNoDisturb(uidInt, noDisturbGroupIds);
        return count;
    }

    @Override
    public int getUnCheckedUUMsgCount(long uidInt) {
        int count = chatMessageRepo.getUncheckedUUMessageCount(uidInt);
        return count;
    }

    @Override
    public int getUnCheckedUCGMsgCount(long uidInt) {
        List<Long> noDisturbGroupIds = userRepo.getNoDisturbGroupIds(uidInt);
        int count = chatMessageRepo.getUncheckedUCGMessageCountExceptNoDisturb(uidInt, noDisturbGroupIds);
        return count;
    }

    @Override
    public String getCircleRemarkName(long userId, long chatGroupId, String name, String remarkName) {
        if(!TextUtils.isEmpty(remarkName)){
            return remarkName;
        }else {
            if(chatGroupId == 0){
                return name;
            }
            CircleMember circleMember = userRepo.getCircleMember(AccountHelper.getUidInt(), userId, chatGroupId);
            if(circleMember != null && !TextUtils.isEmpty(circleMember.getRemarkName())){
                return circleMember.getRemarkName();
            }
            return name;
        }
    }

    @Override
    public IMUserEntity getUser(long id, int type) {
        return userRepo.getUser(AccountHelper.getUidInt(), id, type);
    }

    @Override
    public String getTagConvert(String content, long chatGroupId) {
        try {
            Matcher m= p.matcher(content);
            while (m.find()){
                if("user_id".equals(m.group(1))){
                    long userId = Long.parseLong(m.group(2));
                    if(userId == AccountHelper.getUidInt()){
                        content = content.replace(m.group(0), "我");
                    }else {
                        IMUserEntity user = getUser(userId, Params.USER_PERSON);
                        if(user != null){
                            String showName = getCircleRemarkName(user.getUserId(), chatGroupId
                                    , user.getName(), user.getRemarkName());
                            StringBuilder buffer = new StringBuilder();
                            buffer.append(userDeeplink).append(user.getUserId()).append("\">").append(showName).append("</a>");
                            buffer.append("\u00A0");
                            content = content.replace(m.group(0), buffer.toString());
                        }else {
                            content = content.replace(m.group(0), "有人");
                        }
                    }
                }
            }
        }catch (Exception e){

        }
        return content;
    }

}
